package com.hejia.alauda.persistence.mybatis;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.hejia.alauda.entity.BonusRecord;
import com.hejia.alauda.entity.User;
import com.hejia.alauda.persistence.BaseMapper;
import com.hejia.alauda.utils.Pager;

public interface BonusRecordMapper extends BaseMapper<BonusRecord> {
	
	List<BonusRecord> findBonusRecordsByUser(Map<String, Object> params);
	
	@Update("UPDATE `bonusrecord` SET `consume`=#{consume},`consumeDate`=#{consumeDate},`remaining`=`remaining`-#{remaining} WHERE id=#{id}")
	int updateConsumeById(Map<String, Object> params);
	
	BigDecimal findSumAmountByUser(User user);
	
	List<BonusRecord> findBonusRecordsByPager(@Param("params")Map<String, Object> params,@Param("pager")Pager<BonusRecord> pager);
	
	int findBonusRecordsCountByPager(@Param("params")Map<String, Object> params);
	
	@Update("update `bonusrecord` set `code`=`type`")
	void updateBonusRecordCode();

	@Select("SELECT sum(amount) from `bonusrecord` where user_id = #{userId} and type = #{type}")
	@ResultType(BigDecimal.class)
	BigDecimal amountCountByExample(Map<String,Object>  bonusExample);
	
	@Update("UPDATE `bonusrecord` SET `read`=TRUE WHERE `read`=FALSE AND user_id=#{id}")
	int updateReadByUser(Long id);
	
	@Delete("DELETE FROM `bonusrecord` WHERE `code`=\'REGISTER2015\' AND `user_id`=#{user_id}")
	int removeRegisterBonusRecord(Long id);

	/**
	 * 查询红包记录
	 * @param pager
	 * @param consume
	 * @return
	 */
	List<BonusRecord> findBonusRecords(
			@Param("pager") Pager<BonusRecord> pager, 
			@Param("keyword") String keyword, 
			@Param("consume") Boolean consume);

	/**
	 * 统计红包记录
	 * @param pager
	 * @param consume
	 * @return
	 */
	Integer countBonusRecords(
			@Param("keyword") String keyword, 
			@Param("consume") Boolean consume);

	@Select("select id from bonusrecord where date(expiredDate) = date(now()) and consume = 0 and (oneDayExpirePushAlerted is null or oneDayExpirePushAlerted = false)")
	List<Long> findOneDayExpirePushAlertBonusRecordIds();

	@Update("update `BonusRecord` set modifyDate = now(), oneDayExpirePushAlerted = true where id = #{id}")
	int updateOneDayExpirePushAlertById(@Param("id") Long id);

	@Select("select id from bonusrecord where date(expiredDate) = date(now()) and consume = 0 and (oneDayExpireSmsAlerted is null or oneDayExpireSmsAlerted = false)")
	List<Long> findOneDayExpireSmsAlertBonusRecordIds();

	@Update("update `BonusRecord` set modifyDate = now(), oneDayExpireSmsAlerted = true where id = #{id}")
	int updateOneDayExpireSmsAlertById(@Param("id") Long id);

	@Select("select id from bonusrecord where date(expiredDate) = date_add(date(now()), interval 7 day) and consume = 0 and (sevenDayExpirePushAlerted is null or sevenDayExpirePushAlerted = false)")
	List<Long> findSevenDayExpirePushAlertBonusRecordIds();

	@Update("update `BonusRecord` set modifyDate = now(), sevenDayExpirePushAlerted = true where id = #{id}")
	int updateSevenDayExpirePushAlertById(Long id);
}
